package com.css.attachment.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.css.attachment.service.AttachmentService;
import com.css.exception.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Dbj
 * @date 2024年08月30日  9:37
 */
@RestController
@Slf4j
@RequestMapping("/attachment")
public class AttachmentController {

    @Resource
    private AttachmentService attachmentService;

    /**
     * shp文件上传
     */
    @PostMapping("/uploadShpFile")
    public CommonResult uploadShpFile(List<MultipartFile> file, HttpServletRequest request) {
        String code = request.getHeader("divisionCode");
        String divisionCode = code.split("_")[0];
        if (CollectionUtil.isEmpty(file) || file.size() == 0) {
            return CommonResult.error("不允许上传空文件");
        }
//        if (file.size() != 7) {
//            return CommonResult.error("缺少必须上传的文件");
//        }
        try {
            // 切换到省份数据源
            DynamicDataSourceContextHolder.push("province_"+divisionCode);
            return CommonResult.success(attachmentService.uploadShpFile(file));
        } catch (Exception e) {
            log.error("post list error! message:{}", e.getMessage(), e);
            return CommonResult.error("文件上传失败");
        } finally {
            DynamicDataSourceContextHolder.clear();
        }

    }

    /**
     * 文件上传
     */
    @PostMapping("/uploadFile")
    public CommonResult uploadFile(List<MultipartFile> file, HttpServletRequest request) {
        String code = request.getHeader("divisionCode");
        String divisionCode = code.split("_")[0];
        if (CollectionUtil.isEmpty(file) || file.size() == 0) {
            return CommonResult.error("不允许上传空文件");
        }
        try {
            // 切换到省份数据源
            DynamicDataSourceContextHolder.push("province_"+divisionCode);
            return CommonResult.success(attachmentService.uploadFile(file));
        } catch (Exception e) {
            log.error("post uploadFile error! message:{}", e.getMessage(), e);
            return CommonResult.error("文件上传失败");
        } finally {
            DynamicDataSourceContextHolder.clear();
        }
    }

    /**
     * 文件下载
     *
     * @param attachId 附件id
     */
    @GetMapping(value = "/downLoadFile")
    public void downloadFile(@RequestParam("attachId") String attachId, HttpServletRequest request) {
        try{
            String code = request.getHeader("divisionCode");
            String divisionCode = code.split("_")[0];
            // 切换到省份数据源
            DynamicDataSourceContextHolder.push("province_"+divisionCode);
            attachmentService.downloadFile(attachId);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DynamicDataSourceContextHolder.clear();
        }
    }
}
